/**
 * 季节类型维护
 */
import * as React from 'react'
import { Card, Form, Input, Button, Select, message, Cascader } from 'antd'
import Authentication from '../../../router/Authentication'
import { PageHeaderLayout, FormUtils } from '../../../components'
import { FormComponentProps } from 'antd/lib/form/Form'
import { post, CREATE_SEASON_PARAM, EDIT_SEASON_PARAM } from '../../../service/api'
const FormItem = Form.Item
const Option = Select.Option
export interface Props{
  record: any
  baseInfo: any
  history: any
  centerCategorys: any []
  submitting: boolean
}
export interface State {
  entity: any
}
@Authentication
class SeasonParamForm extends React.Component<Props & FormComponentProps, State> {
  state = {
    entity: {
      ID: ''
    }
  }
  componentDidMount () {
    const { form, record } = this.props
    if(record.ID) {
      let row = record
      row = {
        ...row,
        band_style: [row.dlid==null?"0": row.dlid.toString(), row.xlid==null?"0": row.xlid.toString()],
        seasonname: `${row.seasonid},${row.seasonname}`
      }
      FormUtils.setFieldsValue(form, row)
      this.setState({
        entity: row
      })
    }
  }
  //保存
  handleSave = (e: any) => {
    e.preventDefault()
    this.props.form.validateFieldsAndScroll((err: any, values: any) => {
      if (!err) {
        const { entity: { ID } } = this.state
        const seasonname: string [] = values.seasonname.split(',')
        values = {
          ...values,
          ID,
          dlid: values.band_style[0],
          xlid: values.band_style.length > 1?values.band_style[1]: "0",
          seasonid: seasonname[0],
          seasonname: seasonname[1]
        }
        post(ID?EDIT_SEASON_PARAM: CREATE_SEASON_PARAM, values).then((res: any) => {
          if (res.code == '1') {
            message.success('操作成功！')
            this.props.history.goBack()
          }else {
            message.error(res.errMsg)
          }
        })
      }
    })
  }
  render () {
    const { form, submitting, centerCategorys, baseInfo } = this.props
    const { getFieldDecorator } = form
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 34 },
        sm: { span: 12 },
        md: { span: 15 },
      },
    }
    const submitFormLayout = {
      wrapperCol: {
        xs: { span: 24, offset: 0 },
        sm: { span: 10, offset: 7 },
      },
    }
    return (
      <PageHeaderLayout
        title = "">
        <Card bordered={false}>
          <Form onSubmit={this.handleSave} hideRequiredMark style={{ marginTop: 8 }}>
            <FormItem label="分类" {...formItemLayout}>
              {getFieldDecorator('band_style', { 
                rules: [{ required: true, message: '请选择分类' }]})(
                <Cascader options={centerCategorys} changeOnSelect placeholder="请选择分类"/>
              )} 
            </FormItem>
            <FormItem label="季节名称" {...formItemLayout}>
              {getFieldDecorator('seasonname', { 
                rules: [{ required: true, message: '请选择季节名称' }]})(
                <Select placeholder="请选择季节名称">
                   {baseInfo.map( (info: any, index: number) => (<Option key={index} value={`${info.dtvalue},${info.dtname}`}>{info.dtname}</Option>))}
                </Select>
              )} 
            </FormItem>
            <FormItem label="比例" {...formItemLayout}>
              {getFieldDecorator('rate', { 
                rules: [{ required: true, message: '请输入比例' }]})(
                <Input placeholder="请输入比例"/>
              )} 
            </FormItem>
            <FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" loading={submitting}>
                保存
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={()=>{this.props.history.goBack()}} >返回</Button>
            </FormItem>
          </Form>
        </Card>
      </PageHeaderLayout>
    )
  }
}

export default Form.create()(SeasonParamForm)
